var msgSocketIO={
    _firstLogin:false,
    _initConnect:function(){
        console.log('正在建立连接...');
        try{
            msgsocket  =  new WebSocket('wss://chat.mxzhuawawa.com:7273');
        }catch(e){
            console.log('连接异常 ： '+e);
            return;
        }
        msgSocketIO._wbSocket = msgsocket;
        msgsocket.onopen = function(event) {
            if(!msgSocketIO._firstLogin){

                if(isLogin) {
                    var data={};
                    data._method_= "login";
                    data.user_id = user.uid;
                    data.user_name = user.nickname;
                    data.room_id  = machine_id;
                    data.avatar = user.avatar;
                    data.token =token;
                    msgSocketIO._sendMsg(JSON.stringify(data));
                    msgSocketIO._firstLogin = true;
                } else {
                    var data={};
                    data._method_ = "login";
                    data.room_id  = machine_id;
                    msgSocketIO._sendMsg(JSON.stringify(data));
                    msgSocketIO._firstLogin = true;
                }
            }

            msgsocket.onmessage = msgSocketIO._msgReceive;

            msgsocket.onclose = function(event) {
                console.log('连接关闭.',event);
            };

        };
    },
    _chatMessage:function(msg){
        var data = {};
        data._method_ = "SendPubMsg";
        data.content   = msg;
        data.client_name = user.nickname;
        msgSocketIO._sendMsg(JSON.stringify(data));
    },

    _sendMsg:function(msgBuf){
        if(msgBuf!=null&&msgBuf!='undefined'){
            msgSocketIO._wbSocket.send(msgBuf);
        }else{
            console.log('发送消息为空!');
        }
    },

    _msgReceive:function(event) {
        var data = JSON.parse(event.data);
        console.log(data);
        if(chat.hasOwnProperty('_'+data.type)){
            eval('chat._'+data.type+ '(data)');
        }
    }
}


var chat={
    _sysmsg:function(data){
        var msg='<div class="msg"><p style="color:#f0dc07;background:rgba(131, 115, 203, 0.79);">'+data.content+'</p></div>';
        $(".live-msg-list ").append(msg);
        this.msgfn();
    },
    _SendPubMsg:function(data){
        $("#inputchat").val("");
        $(".showchatbox").addClass("none");
        var msg='<div class="msg"><span>'+data.from_client_name+'：</span><p>'+data.content+'</p></div>';
        $(".live-msg-list ").append(msg);
        this.msgfn();
    },
    _game_result:function(data){
        $(".show_ctrls .ctrlstatr").removeClass("dstartBg");
        var html='<div class="msgitem">'
                    +'<img src="'+data.user_img+'"><span>'+data.content+'</span>'
                +'</div>'
        $(".flymsg").append(html);
        this.init_screen()
    },
    msgfn:function(){
        if($("#chat_hall .msg").length > 100) {
            $("#chat_hall").children().first().remove();
        }
        var scrojh=$("#upchat_hall")[0].scrollHeight;
        $("#chat_hall").scrollTop($("#upchat_hall").scrollTop(scrojh));
    },
    _login:function(data){
        var msg='<div class="msg"><p style="color:#fff;background:rgba(248, 156, 3, 0.78);">'+data.client_name+'进入房间</p></div>';
        $(".live-msg-list ").append(msg);
        this.msgfn();
    },
    _onLineClient:function(data){
        var html="";
        var list=data.client_list;
        $(".onlinenumber").html(data.all_num+"人在房间")
        for(var i=0;i<list.length;i++){
            html+="<li class='onlineitem' data-uid="+list[i]['user_id']+"><img src='"+list[i]['avatar']+"' class='userid"+list[i]['user_id']+"'  data-id='"+list[i]['user_id']+"'></li>";
        }
        $(".onlineuser").html(html)
    },
    init_screen:function(){
        var _top = 0;
        $(".flymsg .msgitem").each(function () {
            var _left = $(window).width() - $(this).width()+400;
            var _height = $(window).height();
            _top =Math.random()*200;
            $(this).css({left: _left, top: _top});
            var time = 12000;
            $(this).animate({left: "-" + _left + "px"}, time, function () {
                $(this).remove(); 
            });
        });
    }
}










